package com.ruoyi.wm.service.impl;

import com.ruoyi.wm.domain.*;
import com.ruoyi.wm.mapper.*;
import com.ruoyi.wm.service.IWmOutStorageService;
import com.ruoyi.wm.vo.WmApparatusVo;
import com.ruoyi.wm.vo.WmMedicineVo;
import com.ruoyi.wm.vo.WmOutStorageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

@Service
public class WmOutStorageServiceImpl implements IWmOutStorageService {
    @Autowired
    private WmMedicineInstorageListMapper wmMedicineInstorageListMapper;
    @Autowired
    private WmApparatusInstorageListMapper wmApparatusInstorageListMapper;
    @Autowired
    private WmApparatusOutstorageListMapper wmApparatusOutstorageListMapper;
    @Autowired
    private WmMedicineOutstorageListMapper wmMedicineOutstorageListMapper;
    @Autowired
    private WmStatisticsMapper wmStatisticsMapper;

    private double medicineValue = 0;
    private double apparatusValue = 0;
    private WmOutStorageVo wmOutStorageVo;
    @Override
    public int addOutStorage(WmOutStorageVo wmOutStorageVo) {
        this.wmOutStorageVo =wmOutStorageVo;
        ArrayList<WmMedicineVo> medicines = (ArrayList<WmMedicineVo>) wmOutStorageVo.getMedicines();
        ArrayList<WmApparatusVo> apparatus = (ArrayList<WmApparatusVo>) wmOutStorageVo.getApparatus();
        if(checkAndAllocateMedicine(medicines)&&checkAndAllocateApparatus(apparatus)){
            //将记录插入入库表
            InsertOutStorage();
            // 将价格插入成本核算表
            InsertStatic();
            return 1;
        }
        else{
            return 0;
        }
    }
    public boolean checkAndAllocateMedicine(ArrayList<WmMedicineVo> medicines){
        medicineValue = 0;
        int length1 = medicines.size();
        ArrayList<WmMedicineInstorageList> medicineStorageList=null;
        for(int i = 0;i<length1;i++){//从第一个药品开始进行出库
            int needNum = medicines.get(i).getNum();//获取本药品的出库数量
            String name = medicines.get(i).getName(); // 获取本药品名以供后面查询其库存
            medicineStorageList = (ArrayList<WmMedicineInstorageList>)
                        wmMedicineInstorageListMapper.selectWmMedicineInstorageListByRemainNum(name);//获取库存记录
            //出库逻辑
            for(int j = 0;j<medicineStorageList.size();j++){//从上到下遍历库存记录

                int remainNum = medicineStorageList.get(j).getRemainNum();//获取本条记录的剩余数量

                if(needNum<=remainNum){//如果需求数量<=剩余数量，直接分配，退出循环
                    medicineStorageList.get(j).setRemainNum(remainNum-needNum);
                    double value =medicineStorageList.get(j).getPrice().multiply(BigDecimal.valueOf(needNum)).doubleValue();//计算出出库成本
                    medicineValue+=value;//加到总成本上
                    needNum=0;
                    break;
                }
                else {//如果需求数量>剩余数量
                    medicineStorageList.get(j).setRemainNum(0);//本次记录剩余全部出库
                    double value =medicineStorageList.get(j).getPrice().multiply(BigDecimal.valueOf(remainNum)).doubleValue();//计算出出库成本
                    medicineValue+=value;//加到总成本上
                    needNum-=remainNum;
                }
            }
            if(needNum!=0){
                return false;
            }
        }
        assert medicineStorageList != null;
        //更新剩余库存数量
        for (WmMedicineInstorageList list: medicineStorageList) {
            wmMedicineInstorageListMapper.updateWmMedicineInstorageList(list);
        }
        return true;
    }
    public boolean checkAndAllocateApparatus(ArrayList<WmApparatusVo> apparatus){
        apparatusValue = 0;
        int length1 = apparatus.size();
        ArrayList<WmApparatusInstorageList> apparatusStorageList=null;
        for(int i = 0;i<length1;i++){//从第一个药品开始进行出库
            int needNum = apparatus.get(i).getNum();//获取本药品的出库数量
            String name = apparatus.get(i).getName(); // 获取本药品名以供后面查询其库存
            apparatusStorageList = (ArrayList<WmApparatusInstorageList>)
                    wmApparatusInstorageListMapper.selectWmApparatusInstorageListByRemainNum(name);//获取库存记录
            //出库逻辑
            for(int j = 0;j<apparatusStorageList.size();j++){//从上到下遍历库存记录

                int remainNum = apparatusStorageList.get(j).getRemainNum();//获取本条记录的剩余数量
                if(needNum<=remainNum){//如果需求数量<=剩余数量，直接分配，退出循环
                    apparatusStorageList.get(j).setRemainNum(remainNum-needNum);
                    double value =apparatusStorageList.get(j).getPrice().multiply(BigDecimal.valueOf(needNum)).doubleValue();//计算出出库成本
                    needNum=0;
                    apparatusValue+=value;//加到总成本上
                    break;
                }
                else {//如果需求数量>剩余数量
                    apparatusStorageList.get(j).setRemainNum(0);//本次记录剩余全部出库
                    double value =apparatusStorageList.get(j).getPrice().multiply(BigDecimal.valueOf(remainNum)).doubleValue();//计算出出库成本
                    apparatusValue+=value;//加到总成本上
                    needNum-=remainNum;
                }
            }
            if(needNum!=0){
                return false;
            }
        }
        assert apparatusStorageList != null;
        //更新剩余库存数量
        for (WmApparatusInstorageList list: apparatusStorageList) {
            wmApparatusInstorageListMapper.updateWmApparatusInstorageList(list);
        }
        return true;
    }
    public void InsertOutStorage(){
        BigDecimal medicineCost = new BigDecimal(medicineValue);
        BigDecimal apparatusCost = new BigDecimal(apparatusValue);
        String doctorName =wmOutStorageVo.getDoctorName();
        String nurseName = wmOutStorageVo.getNurseName();
        String driverName = wmOutStorageVo.getDriverName();
        Date date = new Date();
        ArrayList<WmMedicineVo> medicines = (ArrayList<WmMedicineVo>) wmOutStorageVo.getMedicines();
        ArrayList<WmApparatusVo> apparatus = (ArrayList<WmApparatusVo>) wmOutStorageVo.getApparatus();
        for (WmMedicineVo medicine:medicines){
            WmMedicineOutstorageList record = new WmMedicineOutstorageList();
            record.setTime(new Date());
            record.setDoctorName(doctorName);
            record.setName(medicine.getName());
            record.setNum((long) medicine.getNum());
            record.setTotalPrice(medicineCost);//加入总金额
            wmMedicineOutstorageListMapper.insertWmMedicineOutstorageList(record);
        }
        for(WmApparatusVo apparatusVo:apparatus){
            WmApparatusOutstorageList record = new WmApparatusOutstorageList();
            record.setTime(new Date());
            record.setDoctorName(doctorName);
            record.setName(apparatusVo.getName());
            record.setNum((long) apparatusVo.getNum());
            record.setTotalPrice(apparatusCost);
            wmApparatusOutstorageListMapper.insertWmApparatusOutstorageList(record);
        }
    }
    public void InsertStatic(){
        ArrayList<String> names = new ArrayList<>();
        names.add(wmOutStorageVo.getDoctorName());
        names.add(wmOutStorageVo.getNurseName());
        names.add(wmOutStorageVo.getDriverName());
        BigDecimal medicineCost = new BigDecimal(medicineValue/3);
        BigDecimal apparatusCost = new BigDecimal(apparatusValue/3);
        for(String name:names){
            Calendar calendar = Calendar.getInstance();
            String year = Integer.toString(calendar.get(Calendar.YEAR));
            String month = Integer.toString(calendar.get(Calendar.MONTH)+1);
            WmStatistics statistics = new WmStatistics();
            statistics.setName(name);
            statistics.setMedicineValue(medicineCost);
            statistics.setApparatusValue(apparatusCost);
            statistics.setSum(medicineCost.add(apparatusCost));
            statistics.setMonth(month);
            statistics.setYear(year);
            wmStatisticsMapper.updateWmStatistics(statistics);
        }
    }
}
